<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class DepartmentController extends Controller
{
	//部门首页
	public function getIndex(Request $request){
        //查询数据分页
		// dd($request->all());

        if($request['name']){
            //搜索分页
            $res = DB::table('department')
            ->where('DepartmentName','like','%'.$request['name'].'%')
            ->paginate(10);
        }else{
            //所有分页
            $res = DB::table('department')
            ->paginate(10);
        }
       
        $info = $request -> all();

		return view('department.department.department',['res'=>$res,'info'=>$info]);
	}
    //部门添加页面
    public function getAdd(){
    	//解析视图
    	return view('department.department.add');
    	
    }

    //执行添加
    public function postInsert(Request $request){
        //表单验证
        $this->validate($request, [
        'DepartmentId' => 'required|numeric|between:1001,1999|unique:department,DepartmentId',
        'DepartmentName' => 'required|unique:department,DepartmentName',
        'DepartmentShort' => 'required|alpha_num',
        'Remark' => 'required',
        ],[
        'DepartmentId.required'=>'部门编号不能为空',
        'DepartmentId.unique'=>'部门编号已存在',
        'DepartmentId.numeric'=>'部门编号必须为数字',
        'DepartmentId.between'=>'部门编号必须为1001~1999之间的数字',
        'DepartmentName.required'=>'部门名称不能为空',
        'DepartmentName.unique'=>'部门名称已存在',
        'DepartmentShort.required'=>'部门简称不能为空',
        'DepartmentShort.alpha_num'=>'部门简称必须为字母数字',
        'Remark.required'=>'备注不能为空',
        ]);
    	$res = $request -> all();
   		$info = array_slice($res,0,4);
    	DB::table('department')->insert($info);

    	// // dd($res);
    	// //跳转
    	return redirect('/department')->with('添加成功');

    	// dd($res);
    	//跳转
    	return redirect('/department/department')->with('添加成功');

    }

    //执行删除
    public function getDel($id){
        //删除
        DB::table('department')->where('id',$id)->delete();
        return redirect('/department/department')->with('删除成功');

    }

    //编辑
    public function getEdit($id){
        
        // dd($id);
        $res = DB::table('department')->where('id',$id)->get();

        return view('/department/department.edit',['res'=>$res,'id'=>$id]);

    }
    //执行修改
    public function postUpdate(Request $request){
        $res = $request->all();
        $id = $res['id'];
        DB::table('department')->where('id',$id)->update($res);
        return redirect('/department/department')->with('修改成功');

    }

    //查看详细信息
    public function getLook($id){
        
        $res = DB::table('department')->where('id',$id)->get();
        return view('department/department.look',['res'=>$res,'id'=>$id]);

    }

    
}
